﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChinaClipping.ClippingMaster .Model ;
namespace ChinaClipping.ClippingMaster.Editor
{
    public partial class Dlg_SelectClient : Form
    {
        private static Dlg_SelectClient dlg = new Dlg_SelectClient();

        private List<ClientInfo> _clients;

        private Dlg_SelectClient()
        {
            InitializeComponent();
        }

        public static Dlg_SelectClient GetDialog(List<ClientInfo> clients)
        {
            if (dlg._clients != clients)
            {
                dlg._clients = clients;
                dlg.populateClients();
            }
            return dlg;
        }

        private void populateClients()
        {
            if (this._clients == null)
                return;
            comboBox_Client.Items.Clear();
            foreach (ClientInfo ci in this._clients)
            {
                comboBox_Client.Items.Add(ci);
            }
        }

        private void comboBox_Client_SelectedIndexChanged(object sender, EventArgs e)
        {
            treeView_Class.Nodes.Clear();
            lv_SelectedClasses.Items.Clear();
            ClientInfo client=comboBox_Client .SelectedItem as ClientInfo ;
            if(client!=null)
            {
                foreach (ClassInfo ci in client.Classes)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = ci.Name;
                    tn.Tag = ci;
                    treeView_Class.Nodes.Add(tn);
                    populateClasses(tn, ci);
                }
            }
         }

        private void populateClasses(TreeNode treenode, ClassInfo classinfo)
        {
            foreach (ClassInfo ci in classinfo.SubClasses)
            {
                TreeNode tn = new TreeNode();
                tn.Text = ci.Name;
                tn.Tag = ci;
                
                treenode.Nodes.Add(tn);
                populateClasses(tn, ci);
            }
        }

        private void OK_Button_Click(object sender, EventArgs e)
        {
            //if (this.treeView_Class.SelectedNode == null)
            //{
            //    MessageBox.Show("请选择上传分类！", "无效分类", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            //}
            //else if (this.treeView_Class.SelectedNode.Nodes.Count == 0)
            //{
            //    this.DialogResult = DialogResult.OK;
            //    this.Hide();
            //}
            //else
            //{
            //    if (sender == this.OK_Button)
            //        MessageBox.Show("只能选择最低级别的分类！", "无效分类", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            //}



            if (this.treeView_Class.SelectedNode == null)
            {
                MessageBox.Show("请选择上传分类！", "无效分类", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else if (this.treeView_Class.SelectedNode.Nodes.Count == 0)
            {
                this.DialogResult = DialogResult.OK;
                this.Hide();
            }
            else
            {
                if (sender == this.OK_Button)
                    MessageBox.Show("只能选择最低级别的分类！", "无效分类", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void Cancel_Button_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Hide();
        }

        public List<ClassInfo> SelectedClass
        {
            get
            {
                List<ClassInfo> rtn = new List<ClassInfo>();
                foreach (ListViewItem item in lv_SelectedClasses.Items)
                {
                   rtn.Add (item.Tag as ClassInfo);
                
                }
                return rtn;
            }
        }

        public ClientInfo SelectedClient
        {
            get { return this.comboBox_Client.SelectedItem as ClientInfo; }
        }
        //public string SelectedClassFullPath
        //{
        //    get 
        //    {
        //        if (this.comboBox_Client.SelectedItem != null && this.treeView_Class.SelectedNode != null)
        //        {
        //            return string.Format("[{0}]  {1}",comboBox_Client.Text ,this.treeView_Class .SelectedNode .FullPath);
        //        }
        //        else
        //        {
        //            return null;
        //        }
        //    }
        //}

        private void btn_AddClass_Click(object sender, EventArgs e)
        {
            if (treeView_Class.SelectedNode != null)
            {
                ClassInfo ci = treeView_Class.SelectedNode.Tag as ClassInfo;

                if (ci != null)
                {
                    if (!lv_SelectedClasses.Items.ContainsKey(ci.Id))
                    {
                        ci.FullPath = string.Format("[{0}]  {1}", comboBox_Client.Text, this.treeView_Class.SelectedNode.FullPath);
                        ListViewItem lvi = lv_SelectedClasses.Items.Add(ci.Id, ci.Name, 0);
                        lvi.Tag = ci;
                     
                        lvi.ToolTipText = ci.FullPath;
                    }
                }
            }
        }

        private void btn_RemoveClass_Click(object sender, EventArgs e)
        {
            if (lv_SelectedClasses.SelectedItems != null)
            {
                List<string> idsToRemove = new List<string>();
                foreach (ListViewItem lvi in lv_SelectedClasses.SelectedItems)
                {
                    string id = (lvi.Tag as ClassInfo).Id;
                    idsToRemove.Add(id);
                }
                foreach (string id in idsToRemove)
                {
                    lv_SelectedClasses.Items.RemoveByKey(id);
                }
            }
        }

        public void ClearSelectedCategory()
        {
            lv_SelectedClasses.Items.Clear();
        }

        private void lv_SelectedClasses_DoubleClick(object sender, EventArgs e)
        {
            btn_RemoveClass_Click(null, null);
        }

        private void treeView_Class_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Nodes == null || e.Node.Nodes.Count == 0)
                btn_AddClass_Click(null, null);
        }

        private void treeView_Class_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes == null || e.Node.Nodes.Count == 0)
                btn_AddClass.Enabled = true;
            else
                btn_AddClass.Enabled = false;
        }

        private void lv_SelectedClasses_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (lv_SelectedClasses.SelectedItems != null && lv_SelectedClasses.SelectedItems.Count > 0)
                btn_RemoveClass.Enabled = true;
            else
                btn_RemoveClass.Enabled = false;
        }

       
       

    }
}
